package com.example.android.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.activityViewModels
import com.example.android.R
import com.example.android.activities.MainActivity
import com.example.android.extensions.Services
import android.content.Intent
import android.util.Log
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import com.example.android.models.User
import com.example.android.viewmodels.AnnounceViewModel
import com.example.android.viewmodels.SelectBedViewModel
import com.example.android.viewmodels.SelfInfoViewModel
import com.example.android.viewmodels.SharedViewModel
import kotlinx.android.synthetic.main.fragment_self_info.*

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


/**
 * <p>
 *
 * </p>
 *
 * @author lxy
 * @since 2022-5-30
 */


// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [SelfInfoFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class SelfInfoFragment  : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    private var btn: Button? = null
    private var tv:TextView?=null


    private val _Model : SharedViewModel by activityViewModels()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }




    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        return inflater.inflate(R.layout.fragment_self_info, container, false)

    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment SelfInfoFragment.
         */

        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            SelfInfoFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }



    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        //点击按钮实现页面跳转
        tv = view.findViewById(R.id.name)
        btn = view.findViewById(R.id.button_amend);
        btn?.setOnClickListener(View.OnClickListener() {

            val InfoFragment = SelfInfo2Fragment()
            requireActivity().supportFragmentManager
                .beginTransaction()
                .setCustomAnimations(
                    R.anim.fragment_item_select_in,
                    R.anim.fragment_item_select_out,
                    R.anim.fragment_item_back_in,
                    R.anim.fragment_item_back_out)
                .replace(R.id.content_main_fragment, InfoFragment)
                .addToBackStack(null)
                .commit()

        })




        //通过ViewModel获取数据
        lifecycleScope.launch(Dispatchers.Main) {
            Services.SharedService.GetSelfInfo(_Model)
        }

        //val model: SelectBedViewModel by viewModels()
        _Model.getSelf().observe(this, Observer<User>{ user ->
            // update UI
            val name = view.findViewById<TextView>(R.id.name)
            name.text = user.name
            Log.d(toString(),"yuanben" +user.name)
            val sex = view.findViewById<TextView>(R.id.sex)
            sex.text = user.sex
            val grade = view.findViewById<TextView>(R.id.grade)
            grade.text = user.classes
            val id = view.findViewById<TextView>(R.id.id)
            id.text = user.identity
            val phone = view.findViewById<TextView>(R.id.phone)
            phone.text = user.phone
            val number = view.findViewById<TextView>(R.id.number)
            number.text = user.id.toString()
            val poli_status = view.findViewById<TextView>(R.id.poli_status)
            poli_status.text = user.political
            val college = view.findViewById<TextView>(R.id.college)
            college.text = user.college
            val major = view.findViewById<TextView>(R.id.major)
            major.text = user.major
            val address = view.findViewById<TextView>(R.id.address)
            address.text = user.address

        })

    }

}